﻿$(document).ready(function () {
    $("#city_list").change(function () {
        getDistrict("","");
    });
    $("#city_list").click(function(){
        getDistrict("","");
    });

    $("#district_list").change(function () {
        getTown("");
    });
    $("#district_list").click(function () {
        getTown("");
    });
});

function getDistrict(district_str, town_str) {
    var str = "";
    var city_id = $("#city_list").val();
    if (city_id != null) {
        $("#district_list").empty();
        $("#town_list").empty();
        $.ajax({
            type: "post",
            url: "/Common/GetDistrictsByCID",
            data: { id: city_id },
            success: function (model) {
                if (model.Statu == "ok") {
                    var data = model.Data;
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].DistrictName == district_str) {
                            str += '<option value="' + data[i].ID + '" selected="selected">' + data[i].DistrictName + '</option>';
                        }
                        else {
                            str += '<option value="' + data[i].ID + '">' + data[i].DistrictName + '</option>';
                        }
                    }
                    $("#district_list").append(str);
                    getTown(town_str);
                }
                else if (model.Statu == "err") {
                    $("#district_list").empty();
                    $("#district_list").prepend("<option value='0'>空</option>");
                }
            },
            error: function (model) {
                $("#district_list").empty();
                CastMsg("网络连接异常");
            }
        });
    }

}
function getTown(town_str) {
    var disctrcict_id = $("#district_list").val();
    if (disctrcict_id != null) {
        var str = "";
        $("#town_list").empty();
        $.ajax({
            type: "post",
            url: "/Common/GetTownsByDID",
            data: { id: disctrcict_id },
            success: function (model) {
                if (model.Statu == "ok") {
                    var data = model.Data;
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].TownName == town_str) {
                            str += '<option value="' + data[i].ID + '" selected="selected">' + data[i].TownName + '</option>';
                        }
                        else {
                            str += '<option value="' + data[i].ID + '">' + data[i].TownName + '</option>';
                        }
                    }
                    $("#town_list").append(str);
                }
                else if (model.Statu == "err") {
                    $("#town_list").empty();
                    $("#town_list").prepend("<option value='0'>空</option>");
                }
            },
            error: function (model) {
                $("#town_list").empty();
                CastMsg("网络连接异常");
            }
        });
    }
}
